const {baseController} = require('../../baseController')
const serviceEnter = require('../../../service/serviceEnter')
const {result} = require('../../../util/result/result')
const getRandomString= require('../../../util/radmonStr/radmonStr');
const redis = require('../../../util/redis/redis');
const jwt = require('../../../util/token/jwt');
const {user_rolenum} = require('../../../code/user_rolenum');
const {userRolesService} = require('../../../service/impl/userRolesService/index');
const { userRolesBean } = require('../../../beans/impl/userRolesBean');
const mongoose = require('mongoose');
const { userBean } = require('../../../beans/beansEnter');
const { role_service } = require('../../../service/impl/role_service');
const { role_code } = require('../../../code/role_code/index');
const ObjectId = mongoose.Types.ObjectId;


class userController extends  baseController{
    static sigleIns = new userController()

    static getSigleIns =()=>{
        if(this.sigleIns){
            return this.sigleIns
        }else{
            return new userController()
        }
    }

    constructor(){
        super(serviceEnter.userService.getIns())
        this.userService=serviceEnter.userService.getIns()
        // this.locationProfessionService_obj = serviceEnter.locationProfessionService.getIns()
    }









/* 注册账号 */
async cadduser (req,res){
	let postdata=req.body
	if(postdata.username&&postdata.password){
		if(postdata.username.length===0||postdata.password.length===0){
			res.send(result(false))
            return
		}else{
			postdata.rolenum=2
            let uu= await  this.userService.create(postdata)
    
			// let uu=await daouser.dadduser(postdata)
			if(uu._id){
                let resdata = new userBean()
                let aa =  new userRolesBean()
                let oo={}
             
                aa.userID = uu._id
                // 查找应用普通用户的角色ID
                let role001 =  await role_service.getIns().findOne({"roleTag_str":role_code.role001})
           
                aa.appRoles_arr={}
                aa.appRoles_arr.roleID = role001._id
               
                let ii =    await userRolesService.getIns().create(aa)
                            resdata.uid = uu._id
                            resdata.username = uu.username
                            resdata.appRoles_arr = ii.appRoles_arr
                            resdata.companyRoles_arr = ii.companyRoles_arr
                            resdata.appDynamicsAuths_arr = ii.companyDynamicsAuths_arr
                            resdata.companyDynamicsAuths_arr = ii.companyDynamicsAuths_arr                   
							res.send(result(true,resdata))
			}else{
				res.send(result(false,'注册失败'))
			}
		}
	}else{
		res.send(result(false,'账号密码有误'))
	}
}


/* 登录 */
async  cuserlogin (req,res){
    /* 前端传来的账号密码 */
    let username=req.body.username
    let password=req.body.password
    
    /* 查询用户是否存在 */	
    let hahares
    try {
        hahares   = await  serviceEnter.userService.getIns().findByObj({'username':username,'password':password})
    } catch (error) {
        console.log(error);
        res.send(result(false,'登录异常'))
    }
     
    hahares=hahares[0]

    if(JSON.stringify(hahares)!='{}'&&hahares!=undefined&&hahares!=null){
                 /* 生成token */
                if(password==hahares.password){
                    /* 查询到的账号ID作为token参数 */
                    let uid = hahares._id.toString();
                    /* 生成token */
                     let token = jwt.generateToken(uid);
                    /* 把生成的token保存到redis种 */
                    /* 86400 秒 为1天 */
                    await redis.set(token, JSON.stringify({"id":uid}), '86400', function(err,result){
                        if (err) {
                        console.log(err);
                        return;
                        }else{
                        console.log('保存redis成功:');}
                    })

                    let conditions={
                                    "userID":uid
                                    }

                        let fills = {
                            "key1":{
                                "path":"appRoles_arr.roleID",
                                "populate": {
                                    "path": "roleAuths_arr.auth_ref"
                                  }
                            }
                        }

                  let bb =   await  userRolesService.getIns().findOne(conditions,null,fills)
               
                    let resdata= new userBean()
                    resdata.token=token
                    resdata.uid=uid
                    resdata.username=hahares.username
                    // 应用角色
                    resdata.appRoles_arr = bb.appRoles_arr
                    // 应用动态权限
                    resdata.appDynamicsAuths_arr = bb.appDynamicsAuths_arr  
                    resdata.companyRoles_arr = bb.companyRoles_arr
                    resdata.companyDynamicsAuths_arr = bb.companyDynamicsAuths_arr
    
                    res.send(result(true,resdata))
                }
    }else{
    res.send(result(false))
    }
}





}

module.exports={
    userController
}